package com.sanxiau.dormitory.yang.controller;

import com.sanxiau.dormitory.yang.entity.Dorm;
import com.sanxiau.dormitory.yang.entity.Leave;
import com.sanxiau.dormitory.yang.entity.LoginIn;
import com.sanxiau.dormitory.yang.mapper.AttendanceMapper;
import com.sanxiau.dormitory.yang.service.AttendanceService;
import com.sanxiau.dormitory.yang.service.LeaveService;
import com.sanxiau.dormitory.yang.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/leave")
public class LeaveController {

    @Autowired
    AttendanceService attendanceService;
    @Autowired
    AttendanceMapper attendanceMapper;
    @Autowired
    LeaveService leaveService;

    @RequestMapping("/showLeave")
    public String  showLeave(Model model){
        List<Dorm> hous = attendanceService.queryAllHous();
        model.addAttribute("hous",hous);
        //System.out.println("hous:"+hous);
        return "yang/leave";
    }
    @RequestMapping("/addLeave")
    public String  addLeave(Leave leave, String leavetime,String result, HttpSession session,Model model){
        List<Dorm> hous = attendanceService.queryAllHous();
        model.addAttribute("hous",hous);
        if ("认证成功".equals(result)) {
            ResultInfo resultInfo = leaveService.addLeave(leave, leavetime, session);
            //System.out.println(resultInfo);
            model.addAttribute("message", resultInfo.getMessage());
            return "yang/leave";
        }else {
            model.addAttribute("message", "请先完成人脸信息认证");
            return "yang/leave";
        }
    }

    @RequestMapping("/leavesRecord")
    public String  leavesRecord(HttpSession session,Model model){
        List<Leave> leaves = leaveService.queryLeavesByStu_id(session);
        LoginIn loginIn = attendanceMapper.queryUserByUser_name(session.getAttribute("userName")+"");
        model.addAttribute("rank",loginIn.getRemark());
        model.addAttribute("leaves",leaves);
        return "yang/leavesRecord";
    }
    @ResponseBody
    @RequestMapping("passLeaves")
    public ResultInfo  passLeaves(String ids,HttpSession session){

        //System.out.println("ids:"+ids);
        String [] strings = ids.split(",");
        int [] ints = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            ints[i] = Integer.parseInt(strings[i]);
        }
        ResultInfo resultInfo = leaveService.passLeaves(ints,session);
        return resultInfo;
    }
    @ResponseBody
    @RequestMapping("overLeaves")
    public ResultInfo  overLeaves(String ids,HttpSession session){
       // System.out.println("ids:"+ids);
        String [] strings = ids.split(",");
        int [] ints = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            ints[i] = Integer.parseInt(strings[i]);
        }
        ResultInfo resultInfo = leaveService.overLeaves(ints);
        return resultInfo;
    }
    @ResponseBody
    @RequestMapping("auth")
    public ResultInfo  auth(String base64Data,HttpSession session){
        ResultInfo resultInfo = leaveService.auth(base64Data,session);
        return resultInfo;
    }
}
